package J1939;

import java.util.LinkedList;
import java.util.List;

public interface IJ1939_API {

	static final int DM1_MAXDTCS	 = 20;		//  ��󼤻������
	static final int DM2_MAXDTCS	 = 20;		//  ��󼤻������
	
	static final int CAN_RXFIFO_SIZE = 200;		//  CAN֡���ջ�������С
	static final int CAN_TXFIFO_SIZE = 200;		//  CAN֡���ͻ�������С
	
	static final int PGN_DM1 	=	0xFECA;		//	��ǰ���ϱ�
	static final int PGN_DM2 	=	0xFECB;		//	 

	static final int PF_REQPGN =	0xEA;		// 234,����PGN
	static final int PF_REQACK	= 	0xE8;		// 232,ȷ��PGN
	static final byte ACK		=	0x00;		//		ȷ��֡�еĿ����ֽ� -- OK
	static final byte NACK		=	0x01;		//		ȷ��֡�еĿ����ֽ� -- ��֧���������PGN
	static final byte DENY		=	0x02;		//		ȷ��֡�еĿ����ֽ� -- ��ֹ����
	static final byte NRESP		=	0x03;		//		ȷ��֡�еĿ����ֽ� -- ����Ӧ

	static final int PF_PRIV	=	0xEF;		// 239, �����Զ����PGN,��ָ�����սڵ�
	static final int PF_PRICBC	= 	0xFF;		// 255, �����Զ����PGN��ֻ�ܹ㲥����

	static final int DA_GLOBAL	= 	0xFF;		// 255, �㲥��ַ
	static final int DA_INVALID =	0xFE;		// 254, ��Ч�ڵ��ַ
	static final int DA_NONE	= 	0xFE;		// 254, δָ���ڵ��ַ

	static final int PF_DT		= 	0xEB;		// 235, ����Э�������֡PGN

	static final int PF_CM		= 	0xEC;		// 236, ����Э������ӹ���֡PGN
	static final int CM_RTS		=	0x10;		//		���ӹ���֡�еĿ����ֽ�  -- ������
	static final int CM_CTS		=	0x11;		//		���ӹ���֡�еĿ����ֽ�  -- ��׼���ý���
	static final int CM_EOMACK	=	0x13;		//		���ӹ���֡�еĿ����ֽ�  -- ����ɽ���
	static final int CM_ABORT	=	0xFF;		//		���ӹ���֡�еĿ����ֽ�  -- ��ֹ����
	static final int CM_BAM		= 	0x20;		//		���ӹ���֡�еĿ����ֽ�  -- �����㲥����

	static final int ABORT_REASON_TOOMANY	= 0x01;		// ��ֹԭ�򣺲�֧�ָ�������ӹ���Ự
	static final int ABORT_REASON_NORES		= 0x02;		// ��ֹԭ������������Ҫϵͳ��Դ����ֹ���ӹ���Ự
	static final int ABORT_REASON_TIMEOUT	= 0x03;		// ��ֹԭ����ʱ����ֹ����	
	static final int ABORT_READON_UNKNOWN	= 0x04;		// ��ֹԭ�򣺲���

	// ����CAN֡����
	static final byte CAN_STD	=	0;		// ��׼֡
	static final byte CAN_EXD	=	1;		// ��չ֡

	// ����PGN��ʽ
	static final byte MODE_PDU1	=	1;		// PDU1
	static final byte MODE_PDU2	=	2;		// PDU2

	// ����CAN֡FIFO����������
	static final int CAN_ERR_NO_ERRORS_DU16			= 0;		//  Function is called without errors. Buffer was registered successfully. 
	static final int CAN_ERR_CHNL_DU16				= 14;		//  Unknown CAN channel.
	static final int CAN_ERR_NUM_MSG_DU16			= 18;		//  Invalid number of messages.
	static final int CAN_ERR_BUF_COUNT_DU16			= 50;		//  To less transmit buffer registered. (BODAS-design)
	static final int CAN_ERR_OVERLAP_ADDR_DU16		= 55;		//  Buffer overlaps with buffer registered already.
	static final int CAN_ERR_INVALID_ADDR_DU16		= 60;		//  Invalid address.
	static final int CAN_ERR_CFG_NOTALLOWED_DU16	= 99;		//  Configuration is not allowed.
																//  Note: Configuration has to be made only in function sys_main() (C-API).

	// ����J1939Э��ջ״̬
	static final int J1939_CASTATUS_NONE_DU160		= 0x0000;  	// Controller application is not yet initialized, 
																// or invalid function parameter.
														 
	static final int J1939_CASTATUS_INIT_DU16		= 0x0100;	// Controller application is initialized.
	static final int J1939_CASTATUS_WAIT_CLAIM_DU16	= 0x0200;	// Controller application waits for answers during an address 
																//claim procedure.
														
	static final int J1939_CASTATUS_ADDR_CLAIMED_DU16 =	0x0300;	// Controller application could claim an address. Message traffic 
																// was started.  This is the normal operation state.
														
	static final int J1939_CASTATUS_CANNOT_CLAIM_DU16 =	0x0400;	// Controller application could not claim an address. There was 
																// an address conflict with another controller application. The 
																// message traffic will not be started.
	 
																// The following sub status may be set in addition to the 
																// main status above
	 
	static final int J1939_SUBSTATUS_TIMEOUT_DU16	  =	0x0001;	// The J1939 communication stack could not handle all J1939 messages 
																// within one cycle. 
																//		Possible reasons: 
																//		- The maximum execution time, which is defined with 
																//		  j1939_initComm, is to small. 
																//		- The execution time of one or more callback functions is 
																//		  to high.
	 
	static final int J1939_SUBSTATUS_RXOVFL_DU16	  =	0x0002;	// A J1939 CAN receive buffer overflow occured. One or more 
																// CAN RX message(s) may be lost. 
																// 		Possible reasons: 
																//		- The registered CAN RX buffer on the corresponding 
																//		  CAN channel is to small. 
																//		- The J1939 cycle time is to high. Therefore to many 
																//		  CAN messages will be received between two J1939 cycles

	static final int J1939_SUBSTATUS_TXOVFL_DU16 = 0x0004;		// A J1939 CAN transmit buffer overflow occured. One or more 
																// CAN RX message(s) may be lost. 
																// 		Possible reasons: 
																//		- The registered CAN TX buffer on the corresponding 
																//		  CAN channel is to small. 

	// ������Ϸ�Χ
	static final long J1939_SPN_ALL_DU32 			= 0xFFFFFFFFl;	
	static final int J1939_FMI_ALL_DU8				= 0xFF;


	// ����J1939 �������״̬
	static final int J1939_BOXSTATUS_NONE_DU16 			= 0x0000;	// Message databox is not yet initialized.
	static final int J1939_BOXSTATUS_INIT_DU16 			= 0x0100;	// Message databox is initialized.
	static final int J1939_BOXSTATUS_VALID_DU16 		= 0x0200;	// The data within the receive message databox are valid.

	static final int J1939_BOXSTATUS_VAL_OVFL_DU16 		= 0x0201;	// The data within the receive message databox are valid, 
																	// but incomplete. A buffer overflow occurs (received DM1/2 
																	// msg was to long).

	static final int J1939_BOXSTATUS_INV_SEQNBR_DU16 	= 0x0301;	// Databox data are invalid, wrong sequence nbr received.
	static final int J1939_BOXSTATUS_INV_ABORT_DU16  	= 0x0302;	// Databox data are invalid, rx/tx transfer aborted.
	static final int J1939_BOXSTATUS_INV_TIMEOUT_DU16 	= 0x0303;	// Databox data are invalid, timeout occurs.
	static final int J1939_BOXSTATUS_INV_OVFL_DU16 	 	= 0x0304;	// Databox data are invalid, overflow error.

	static final int J1939_BOXSTATUS_PENDING_DU16		= 0x0400;	// ��������������ͣ���������������δ���
	static final int J1939_BOXSTATUS_BAM_RX_DU16		= 0x0500;	// Receiving of a BAM message is in progress.
	static final int J1939_BOXSTATUS_BAM_TX_DU16		= 0x0600;	// Transmitting of a BAM message is in progress
	static final int J1939_BOXSTATUS_UNPACK_DU16		= 0x0700;	// ���ڵ��ûص�����	

	static final int J1939_BOXSTATUS_RTS_DU16			= 0x0800;	// �ѷ���RTS֡���ȴ�����CTS֡ �����յ�RTS
	static final int J1939_BOXSTATUS_CTS0_DU16			= 0x0900;	// ���յ�CTS(0)���ӱ���֡���ȴ�����CTS
	static final int J1939_BOXSTATUS_DT_TX_DU16			= 0x0A00;	// ���յ�CTS֡�����ڷ��� DT(s)֡
	static final int J1939_BOXSTATUS_WAIT_EOM_DU16		= 0x0B00;	// ����DT֡�ѷ��ͣ��ȴ�����EndOfMsgAck֡

	// ���庯������ֵ
	static final int J1939_RC_NO_ERRORS_DU16			= 0; 		// Function was executed without errors. 
	static final int J1939_RC_INVALID_ADDR_DU16			= 1;		// Address to data array is Invalid.
	static final int J1939_RC_INV_DATABOX_DU16			= 2;		// Invalid databox number.
	static final int J1939_RC_INV_PGN_DU16				= 3;		// Invalid parameter group number (PGN).
	static final int J1939_RC_INV_SRC_DU16				= 4;		// Invalid source address.
	static final int J1939_RC_INV_TIMEOUT_DU16			= 5;		// Invalid timeout value.
	static final int J1939_RC_INV_DATA_LEN_DU16			= 6;		// Invalid data length.
	static final int J1939_RC_INV_DATA_PTR_DU16			= 7;		// Invalid address pointer.
	static final int J1939_RC_INV_STARTTIMEOUT_DU16		= 9;		// Invalid start up timeout value.
	static final int J1939_RC_INV_CYCLE_DU16			= 10;		// Invalid cycle time.
	static final int J1939_RC_INV_OFFSET_DU16 			= 11;		// Invalid offset time.
	static final int J1939_RC_INV_PRIO_DU16				= 12;		// Invalid priority.

	static final int J1939_RC_PGN_SRC_EXIST_DU16		= 20;		// Another message box with the same PGN and source address is 
																	// alraedy defined.
														
	static final int J1939_RC_PGN_PRIO_EXIST_DU16		= 21;		// Another message box with the same PGN and priority is 
																	// alraedy defined.
														
	static final int J1939_RC_CFG_NOTALLOWED_DU16		= 22;		// Function must be called before j1939_initComm(), but it was 
																	// called afterwards.

	static final int J1939_RC_INV_BOX_STATUS_DU16		= 30;		// Invalid databox status. Databox is not yet initialized.
	 
	static final int J1939_RC_INV_SPN_DU16 				= 40;		// Invalid suspect parameter number.
	static final int J1939_RC_INV_FMI_DU16				= 41;		// Invalid failure mode indicator.

	static final int J1939_RC_DM1_FULL_DU16				= 42;		// Failure could not be stored in DM1 list, because maximum 
																	// number of failures reached.
							
	static final int J1939_RC_DM2_FULL_DU16 			= 43;		// Failure could not be stored in DM2 list, because maximum 
																	// number of failures reached.
	 
	static final int J1939_RC_SPN_NOT_FOUND_DU16		= 44;		// Failure could not be found within the DM1 list.

	static final int J1939_RC_INV_ECU_ADDR_DU16			= 50;		// Invalid controller (ECU) address.
	static final int J1939_RC_INV_DM_TYPE_DU16 			= 51;		// Invalid DM failure type.

	static final int J1939_RC_INV_LIST_SIZE_DU16		= 60;		// Invalid failure list size.
	static final int J1939_RC_INV_EE_PAGE_DU16			= 61;		// Invalid eeprom page number.
	static final int J1939_RC_INV_EE_IDX_DU16			= 62;		// Invalid eeprom page index.

	static final int J1939_RC_INIT_MISSING_DU16			= 63;		// Function is not possible, because j1939_initFailureList() 
																	// was not yet executed.

	static final int J1939_RC_EEP_OVERFLOW_DU16			= 70;		// Failures could not be deleted, because internal EEPROM 
																	// queue overflow.

	static final int	J1939_RC_DM1_NOT_EXIST_DU16		= 0;		// DM1 failure does not exist within DM1 failure list. 
	static final int	J1939_RC_DM1_EXIST_DU16			= 1;		// DM1 failure(s) exist(s) within DM1 failure list.


	// ������ϱ�����
	static final int J1939_LIST_DM1_DU16				= 0x01; 	// DM1 failure list, use CM version 3 for CM-Bit=1
	static final int J1939_LIST_DM2_DU16 				= 0x02;		// DM2 failure list, use CM version 3 for CM-Bit=1
	static final int J1939_LIST_DM1_V3_DU16				= 0x31; 	// DM1 failure list, use CM version 3 for CM-Bit=1
	static final int J1939_LIST_DM2_V3_DU16 			= 0x32;		// DM2 failure list, use CM version 3 for CM-Bit=1
	static final int J1939_LIST_DM1_V1_DU16 			= 0x11;		// DM1 failure list, use CM version 1 for CM-Bit=1
	static final int J1939_LIST_DM2_V1_DU16 			= 0x12;		// DM2 failure list, use CM version 1 for CM-Bit=1
	static final int J1939_LIST_DM1_V2_DU16 			= 0x21;		// DM1 failure list, use CM version 2 for CM-Bit=1
	static final int J1939_LIST_DM2_V2_DU16				= 0x22;		// DM2 failure list, use CM version 2 for CM-Bit=1


	// ��ʱ���� 
	static final int TIMEOUT_T1							= 750;			// T1
	static final int TIMEOUT_RXBAM_TO_RXDT				= TIMEOUT_T1;	// BAM:		����BAM֡���һ��DT֮֡��ĳ�ʱֵ
	static final int TIMEOUT_RXDT_TO_RXDT				= TIMEOUT_T1;	// BAM&RTS:����DT����֡�ĳ�ʱֵ

	static final int TIMEOUT_T2							= 1250;			// T2
	static final int TIMEOUT_TXCTS_TO_RXFIRSTDT			= TIMEOUT_T2;	// ���շ����ͳ�CTS֡��ȴ���һ��DT֡����ĳ�ʱֵ

	static final int TIMEOUT_T3							= 1250;			// T3
	static final int TIMEOUT_TXRTS_TO_RXCTS				= TIMEOUT_T3;	// ���ͷ�������RTS�����յ�CTS֡�ĳ�ʱֵ
	static final int TIMEOUT_TXDT_TO_RXCTS				= TIMEOUT_T3;	// ���ͷ�������CTS�ڼ����һ֡�����յ���һ��CTS֡��ĳ�ʱֵ
	static final int TIMEOUT_TXDT_TO_RXEOM				= TIMEOUT_T3;	// ���ͷ�������������DT֡���ȴ�����EndOfMessageAck֡�ĳ�ʱֵ

	static final int TIMEOUT_T4							= 1050;			// T4
	static final int TIMEOUT_RXCTS0_TO_RXCTS			= TIMEOUT_T4;	// ���ͷ������յ�CTS(0)����������������һ��CTS֡�ĳ�ʱֵ

	static final int TIMEOUT_Th							= 500;			// Th
	static final int TIMEOUT_TXCTS0_TO_TXCTS			= TIMEOUT_Th;	// ���շ�������CTS(0)��������֡������CTS֡�ĳ�ʱֵ

	static final int TIMEOUT_Tr							= 200;			// Tr
	static final int TIMEOUT_RX_TO_RESP					= TIMEOUT_Tr;	// ���ͷ������շ����յ�֡�󵽷���Ӧ��֮֡��ĳ�ʱֵ

	static final int BAM_TXDT_TO_TXDT_MIN				= 50;			// BAM���ͷ�: ֡����Сʱ����
	static final int BAM_TXDT_TO_TXDT_MAX				= 200;			// BAM���ͷ���֡�����ʱ����
	
// �ӿڷ���
	
	// �ҽ�CAN֡���ջ����� 
	public int can_registerRxBuf(
			byte chnl_u8,                            //	CANͨ����, 0 -- n;
			List<can_Message_ts> buf_pas,        //	CAN֡���ջ�����FIFO����
			short numMsg_16                            //	���ջ�������С��FIFO������
	);
	
	// �ҽ�CAN����FIFO������
	public int can_registerTxBuf(
			byte chnl_u8,                            //	CANͨ����, 0 -- n;
			List<can_Message_ts> buf_pas,        //	CAN֡���ͻ�����FIFO����
			short numMsg_16                            //	���ջ�������С��FIFO������
	);

	// J1939�����ʼ��
	public int j1939_initComm(
			byte canChnl_u8,                        // 	CANͨ����, 0 -- n
			short ownAddr_u8,                        // 	��CAN�ڵ��ַ, 0x00 -- 0xFD
			byte priority_u8,                        // 	J1939��������ȼ��� 0 -- 255
			short cycleTime_u8,                    // 	J1939������������� �� 1 -- 90ms, �Ƽ�10ms
			byte maxTime_u8                        // 	J1939���������ʱ�����������ڵı��ʵ����ֵ��1 -- 99�� �Ƽ�20%
	);

	// �ҽ�J1939��Ϣ����������
	public int  j1939_registerRxDataboxes(
			//j1939_rxMsg_ts *mem_pas ,				// 	 
			J1939_rxMsg_ts[] mem_pas,                //	��������������
			short num_u16                            // 	J1939��Ϣ���������������С�������������
	);

	// �ҽ�J1939��Ϣ����������
	public int j1939_registerTxDataboxes(
			//j1939_txMsg_ts *mem_pas , 			// 		
			J1939_txMsg_ts[] mem_pas,                //	��������������
			short num_u16                            // 	J1939��Ϣ��Ϣ���������������С�������������
	);
	
	// ��ʼ��J1939��Ϣ����������
	public int  j1939_initRxDatabox(
			short boxNum_u16,                        //	��Ϣ��������������, 1 -- n
			int pgn_u32,                            //	��������Ҫ���յ�PGN��0 -- 0x3FFFF (18λ��
			short src_u8,                            //	��������Ҫ���յ�PGN�ķ���Դ��ַ�� 0x00 -- 0xFD
			int timeout_u16,                        //	��Ϣ�������ڣ�ms), 10 -- 65000��0��ʾ�������Խ�������
			int startTimeout_u16,                    //	ϵͳ�������һ�ν���������Ϣ��ʱ�ޣ�ms)��10 -- 65000��0��ʾ��ʱ��
			short lenMax_u16,                        //	��Ϣ��������󳤶ȣ��ֽ������� 1 -- 196
			Object data_pau8,                        //	ָ����Ϣ������
			IRxDBoxCallback func_pf,                //	���յ�����Ϣʱ���õĻص�����
			Object pCallbackArg                    //  ���ûص�����ʱʹ�õĲ���ֵ
	);
	
	// ��ʼ��J1939��Ϣ���������� 
	public int j1939_initTxDatabox(
			short boxNum_u16,                        //	��Ϣ��������������, 1 -- n
			int pgn_u32,                            // 	��������Ҫ���͵�PGN��0 -- 0x3FFFF (18λ��
			byte prio_u8,                            // 	���ʹ���Ϣ�����ȼ��� 0 -- 7 ��Ĭ��Ϊ6
			int cycle_u16,                        //	��Ϣ�������ڣ�ms), J1939�������� -- 65000�� 0��ʾ�������Է�����Ϣ��ֻ��ͨ��j1939_SendDatabox���ͣ�
			int offset_u16,                        //	��Ϣ�״η���ʱ����ǰ����ms), 0 -- cycle_u16, ���ڱ�����ͬ���ڷ�����Ϣ��������߹��ء�
			short lenAct_u16,                        //	��Ϣ��������ʵ�����ݳ��ȣ��ֽ������� 1 -- 196
			byte[] data_pau8,                        //	ָ����Ϣ������
			ITxDBoxCallback func_pf,                // 	���ʹ���Ϣǰ���õĻص�����
			Object pCallbackArg                        // 	���ûص�����ʱʹ�õĲ���ֵ
	);
	
	// ��ȡJ1939����������״̬
	public int  j1939_getRxDataboxStatus(
			short boxNum_u16,                        //	���������������� 1 -- n
			int[] msgLen_pu16                        //	����ʱ������������ѽ������ݳ���
	);
	
	// ����ָ��������
	public int  j1939_sendDatabox(
			short boxNum_u16                        //	���������������� 1 -- n
	);
	
	// 	��ȡJ1939Э��ջ״̬ 
	public int j1939_getStatus(
			byte canChnl_u8                        //	CANͨ����
	);
	
	// ��������PGN
	public int  j1939_sendRequestPGN(
			short destAddr_u8,                        // ������ڵ��ַ��0--253�� 255��
			int pgn_u32                            // ������PGN ( 0 -- 0x003FFFF)
	);
	
	// ׼�����������ڵ���ϱ�
	public int  j1939_initDiagnostic(
			short srcAddr_u8,                        //	���ڵ�Ҫ������DM1/DM2���ϱ�ĵĹ���Դ���������ڵ㣩��ַ
			int dmType_u16,                        //	Ҫ���յĹ��ϱ����ͼ�CM=1ʱ��ת�������汾����CM=0,����ʹ��ת�������汾4.
			J1939_dtcList_ts dtcList_ps,            //	ָ��Ӧ�ö���Ĺ��ϱ�
			short boxNum_u16,                    //	����DM1/DM2����Դ��ʹ�õĽ�����������
			IRxDBoxCallback func_pf,                //	��������DM1/DM2������Ϣ����õĻص�����
			Object pCallbackArg                        //	���ûص�����ʱʹ�õĲ���ֵ
	);
	
	// ���������ڵ㷢����DM1��DM2���ϱ� 
	public int j1939_getDiagnostic(
			short destAddr_u8,                        //	������ڵ��ַ
			int dmType_u16                            //	������Ĺ�������
	);
	
	//	��ʼ����������Ӧ�õ�DM1��DM2���ϴ���
	public int  j1939_initFailureList(
			J1939_failure_ts listFailure_pas,            //	ָ�� j1939_failure_ts���͵Ĺ��ϱ�
			short listSize_u16,                        //	���ϱ��С��������, ��Чֵ��Χ 1 -- 85�������ڿ����ڴ漰EEPROM
			short eePage_u16,                            //	����DM2���ϱ��EEPROMҳ�ţ�1 -- ���������������Ӳ���ľ����������
			short eeIndex_u16,                            //	DM2���ϱ���EEPROMҳ�е���ʼλ�ã�������������Чֵ��Χ 0 -- 255�������뱣֤�������ϱ��ܴ�����EEPROMҳ�С�ÿ�����ϱ���ռ��4���֡�
			short txBoxDM1_u16,                        //	����DM1���ϱ��������ţ��������䲻����j1939_initTxDatabox()��ʼ��
			short txBoxDM2_u16                            //	����DM2���ϱ��������ţ��������䲻����j1939_initTxDatabox()��ʼ��
	);
	
	// �ӱ��ڵ��DM1���ϱ���ɾ��1������������й���
	public int j1939_resetFailureDM1(
			int spn_u32,                                //	���ϵ�SPN, ��Чֵ��Χ 0 -- 524278��ָ��ΪJ1939_SPN_ALL_DU32 (= 0xFFFFFFFF)��ɾ�����й���
			byte fmi_u8                                    //	����ģʽ����Чֵ��Χ 0 -- 31�� ָ��ΪJ1939_FMI_ALL_DU8 (= 0xFF) ʱɾ��ָ��SPN������ģʽ�Ĺ���
	);
	
	// �ӱ��ڵ��DM2���ϱ���ɾ�����й���
	public int j1939_resetFailureDM2(
	);

	// �ڱ��ڵ��DM1���ϱ������1������
	public int  j1939_setFailureDM1(
			int spn_u32,                                //	���ϵ�SPN
			byte fmi_u8,                                //	����ģʽ
			byte lampState_u8,                            //	����ָʾ��״̬��ÿ��λ��Ӧһ��ָʾ�ƣ�00 -- ָʾ���� 01 -- ָʾ������
			byte lampFlash_u8                            //	ָʾ����˸״̬��ÿ��λ��Ӧһ��ָʾ����˸״̬��00 -- ���� 01 -- ����  11 -- ����˸��
	);
	
	// ��鱾�ڵ�Ĺ��ϱ����Ƿ����ָ���Ĺ���
	public int  j1939_testFailureDM1(
			int spn_u32,                                //	�������ϵ�SPN
			byte fmi_u8                                    //	�������ϵ�ģʽ
	);
	
	// ��ָ�����ڱ������Ե��õĺ�����ִ��J1939Э��
	public void j1939_CycAction();
	
}


//���嵥�����������ṹ
//typedef struct _failureLink_ts {
//	j1939_failure_ts		*pFailure;				// ָ����ϱ���
//	struct _failureLink_ts	*pNext;					// ָ�����е���һ��
//} failureLink_ts;




